﻿using System;
using System.Drawing;
using DC2010.Objects;
using DC2010.Objects.Base;
using DC2010.Objects.Data;

namespace DC2010.Effects
{
    public class I_PutItemInInventory_Effect : EffectLogic
    {
        public I_PutItemInInventory_Effect(EffectData ed):base(ed)
        {
            ShortName = "Put item in inventory";
            LongName = "Effect will put item in inventory to most convenient place.";
            Id = "I_PUT_ITEM_IN_INVENTORY_EFFECT";
            EffectType = EffectType.Instant;
        }

        public override void Instant(BaseObject aInvoker, BaseObject aTarget)
        {
            if ((aTarget is ItemData) && (aInvoker is CreatureData))
            {
                CreatureData c = (CreatureData) aInvoker;
                ItemData i = (ItemData)aTarget; // item i want to place...
				
				PutItemToInventory(i, c);
				

                //check if hand is empty
                ItemData sLeft = c.L.Possession(CarryLocations.LeftHand);
                ItemData sRight = c.L.Possession(CarryLocations.RightHand);

                if ((sLeft != null)&&(sRight != null))
                {
                    Root.I.GameSystem.InGameLog(DateTime.Now.ToLongTimeString() + " - NEED AN EMPTY HAND TO PROCESS THIS SPELL.", Color.White);
                    return;
                }

                //get item id
                string pItemId = Data.Params.Find(A => A.Key == "ITEM_ID").Value;

                //create new item
                ItemData iNew = Root.I.ItemSystem.GetInfo(pItemId).CreateData();

                if (iNew==null) return;

                if (sLeft==null) c.Inventory.PutItemToPosition(CarryLocations.LeftHand, iNew.Id);
                if (sRight == null) c.Inventory.PutItemToPosition(CarryLocations.RightHand, iNew.Id);
            }

            base.Instant(aInvoker, aTarget);
        }
		
		
		
        /// <summary>
        /// Puts item into inventory.
        /// 
        /// Every item has a type.
        /// This is called when an item is puted on player face....
        /// Base idea came from here: http://www.dungeon-master.com/forum/viewtopic.php?f=53&t=28488&start=0
        /// </summary>
        /// <param name="i"></param>
        public bool PutItemToInventory(ItemData i, CreatureData c)
        {
            //ItemLogic i = Root.I.ItemSystem.GetData(s).L;

            if (i != null)
            {
                ItemType it = Root.I.ItemSystem.GetInfo(i.InfoId).ItemType;
                bool bHandled = false;

//                if (it == ItemType.Ammo)
  //                  bHandled = PutItemToInventory_Projectile(i,c);      // there exists prefered locations, quivers, ....

                if (it == ItemType.Helm || it == ItemType.Torso || it == ItemType.Legs || it == ItemType.Boots || it == ItemType.Neck)
                    bHandled = PutItemToInventory_Clothing(i,c);        // Put it on self....

                if (it == ItemType.Valuable)
                    bHandled = PutItemToInventory_Valuable(i,c);        // Money box prefered...

                if (it == ItemType.Key)
                    bHandled = PutItemToInventory_Key(i,c);             // Keyring prefered...


                if ((!bHandled)&&(c.Inventory.HasFreeSpot))
                {
                    bool bResult = c.Inventory.PutItemToFirstFreePosition(i.Id);
                    if (bResult) return bResult;
                }

                return bHandled;
            }

            return false;
        }

        public bool PutItemToInventory_Clothing(ItemData i, CreatureData c)
        {
            ItemInfo ii = Root.I.ItemSystem.GetInfo(i.InfoId);

            if (ii.ItemType == ItemType.Helm)
            {
                if (c.Inventory.GetItemAtPosition(CarryLocations.Head)==null)
                {
                    c.Inventory.PutItemToPosition(CarryLocations.Head, i.Id);
                    return true;
                }
            }

            if (ii.ItemType == ItemType.Torso)
            {
                if (c.Inventory.GetItemAtPosition(CarryLocations.Body)==null)
                {
                    c.Inventory.PutItemToPosition(CarryLocations.Body, i.Id);
                    return true;
                }
            }

            if (ii.ItemType == ItemType.Legs)
            {
                if (c.Inventory.GetItemAtPosition(CarryLocations.Legs)==null)
                {
                    c.Inventory.PutItemToPosition(CarryLocations.Legs, i.Id);
                    return true;
                }
            }

            if (ii.ItemType == ItemType.Boots)
            {
                if (c.Inventory.GetItemAtPosition(CarryLocations.Feet)==null)
                {
                    c.Inventory.PutItemToPosition(CarryLocations.Feet, i.Id);
                    return true;
                }
            }

            if (ii.ItemType == ItemType.Neck)
            {
                if (c.Inventory.GetItemAtPosition(CarryLocations.Necklace)==null)
                {
                    c.Inventory.PutItemToPosition(CarryLocations.Necklace, i.Id);
                    return true;
                }
            }

            return false;
        }

        private bool PutItemToInventory_Valuable(ItemData i, CreatureData c)
        {
            int iContainerId = c.Inventory.GetFirstFreeContainer();
            if (iContainerId != -1)
            {
                bool bResult = c.Inventory.PutItemDirectlyIntoContainerItem(iContainerId, i.Id);
                return bResult;
            }

            iContainerId = c.Inventory.GetFirstFreeContainer(ItemType.Valuable);

            if (iContainerId != -1)
            {
                bool bResult = c.Inventory.PutItemDirectlyIntoContainerItem(iContainerId, i.Id);
                return bResult;
            }
            //no keyring in inventory


            //check generic containers
            iContainerId = c.Inventory.GetFirstFreeContainer();
            if (iContainerId != -1)
            {
                bool bResult = c.Inventory.PutItemDirectlyIntoContainerItem(iContainerId, i.Id);
                return bResult;
            }

            //put item into inventory, no container
            bool bResult2 = c.Inventory.PutItemToFirstFreePosition(i.Id);
            return bResult2;
        }

        public bool PutItemToInventory_Key(ItemData i, CreatureData c)
        {
            // Keyring will have effect, TRY UNLOCK which will check keyhole which u are
            // standing before and if some of key mathes it will use that key.
            // 

            // Logic is this:
            // 
            // If u have a key we are looking for keyring or any other item,
            // which can hold keys. Generic containers included. If we will 
            // find nothing we will return false.
            // 
            // Keyring can be placed only in inventory so find there
            // 
            int iContainerId = c.Inventory.GetFirstFreeContainer(ItemType.Key);
            if (iContainerId != -1)
            {
                bool bResult = c.Inventory.PutItemDirectlyIntoContainerItem(iContainerId, i.Id);
                return bResult;
            }

            iContainerId = c.Inventory.GetFirstFreeContainer(ItemType.Key);

            if (iContainerId != -1)
            {
                bool bResult = c.Inventory.PutItemDirectlyIntoContainerItem(iContainerId, i.Id);
                return bResult;
            }
            //no keyring in inventory


            //check generic containers
            iContainerId = c.Inventory.GetFirstFreeContainer();
            if (iContainerId != -1)
            {
                bool bResult = c.Inventory.PutItemDirectlyIntoContainerItem(iContainerId, i.Id);
                return bResult;
            }

            //put item into inventory, no container
            bool bResult2 = c.Inventory.PutItemToFirstFreePosition(i.Id);
            return bResult2;
        }		
		
		
		
		
    }
}
